package com.cheers.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;

import com.cheers.common.Constants;
import com.cheers.common.ResultListModel;
import com.cheers.common.ResultModel;
import com.cheers.service.IntegralService;
import com.cheers.util.VerifyUtil;

public class IntegralAction extends BaseAction {

	private static final long serialVersionUID = 2398886627455914709L;

	@Resource
	private IntegralService integralService;
	
	private String userId;
	private String channel;
	private String point;
	private String pageIndex;
	
	
	/**
	 * 积分排行榜
	 * @return
	 * @throws Exception
	 */
	public String rank() throws Exception {
		//验证
		if (StringUtils.isEmpty(pageIndex)) {
			pageIndex = "0";
		}
		if (!VerifyUtil.numberTest(pageIndex)) {
			result = new ResultModel(Constants.NO, Constants.PAGE_NUM_DESC, "");
			return SUCCESS;
		}
		
		int page = Integer.parseInt(pageIndex);
		//排行榜
		List<Map<String, Object>> list = integralService.rank(page, Constants.PAGESIZE);
		if (list!=null && !list.isEmpty()) {
			result = new ResultListModel(list, pageIndex, Constants.PAGESIZE, list.size());
		} else {
			result = new ResultListModel(new Object[0], pageIndex, Constants.PAGESIZE, 0);
		}
		return SUCCESS;
	}
	
	/**
	 * 更新积分
	 * @return
	 * @throws Exception
	 */
	public String addPoint() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(channel) || (!Constants.CHANNEL_YOUMI.equals(channel) && !Constants.CHANNEL_DIANLE.equals(channel))) {
			result = new ResultModel(Constants.NO, Constants.POINT_CHANNEL_DESC, "");
			return SUCCESS;
		}
		if (!VerifyUtil.numberTest(point)) {
			result = new ResultModel(Constants.NO, Constants.POINT_NUM_DESC, "");
			return SUCCESS;
		}
		if (!isPost("addIntegral")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		int integral = Integer.parseInt(point);
		//积分累计
		String data = integralService.addPoint(userId, channel, integral);
		if (data == null) {
			result = new ResultModel(Constants.USER_CODE, Constants.USER_DESC, "");
			return SUCCESS;
		}
		//获取用户总积分
		Long total = integralService.getTotalPoint(userId);
		result = new ResultModel(Constants.OK, Constants.OK_DESC, String.valueOf(total));
		return SUCCESS;
	}
	
	/**
	 * 获取用户总积分
	 * @return
	 * @throws Exception
	 */
	public String totalPoint() throws Exception {
		//验证
		if (StringUtils.isEmpty(userId)) {
			result = new ResultModel(Constants.NO, Constants.USERID_DESC, "");
			return SUCCESS;
		}
		
		//获取用户总积分
		Long total = integralService.getTotalPoint(userId);
		result = new ResultModel(Constants.OK, Constants.OK_DESC, String.valueOf(total));
		return SUCCESS;
	}

	
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getChannel() {
		return channel;
	}
	public void setChannel(String channel) {
		this.channel = channel;
	}
	public String getPoint() {
		return point;
	}
	public void setPoint(String point) {
		this.point = point;
	}
	
}
